import util from '@/libs/util';
import Cookies from 'js-cookie';
import CONSTANTS from '@/config/constants';
import getDate from '@/util/getDate.js';
import memberList from '@/views/components/member/member.vue';

export default {
    name: 'exportable-table',
    components: { memberList },
    data () {
        return {
            columns: [
                {
                    type: 'selection',
                    width: 60,
                    align: 'center'
                },
                {
                    title: '主题',
                    key: 'themeid',
                    render: (h, params) => { return h('div', {}, params.row.theme.name); }
                }, {
                    title: '奖项',
                    key: 'diplomasid',
                    render: (h, params) => { return h('div', {}, params.row.diplomas.name); }
                }, {
                    title: '获奖人名称',
                    key: 'diplomasid',
                    render: (h, params) => { return h('div', {}, params.row.member.realname); }
                }, {
                    title: '获奖人联系电话',
                    key: 'diplomasid',
                    render: (h, params) => { return h('div', {}, params.row.member.phone); }
                }, {
                    title: '奖状生成时间',
                    key: 'createtime',
                    render: (h, params) => { return h('div', {}, getDate.getDate(params.row.createtime, 'time')); }
                }, {
                    title: '奖状',
                    key: 'createtime',
                    render: (h, params) => {
                        return h('a', {
                            on: {
                                click: () => {
                                    this.imgUrl = CONSTANTS.SERVER_HOST + params.row.imgpath;
                                    this.showImg = true;
                                }
                            }
                        }, '查看');
                    }
                },
                {title: '操作',
                    key: 'action',
                    width: 200,
                    align: 'center',
                    render: (h, params) => {
                        return h('div', [
                            h('Button', {
                                props: {
                                    type: 'error',
                                    size: 'small'
                                },
                                style: {
                                    marginRight: '5px'
                                },
                                on: {
                                    click: () => {
                                        this.$Modal.confirm({
                                            title: '提示',
                                            content: '<p>确定删除？</p>',
                                            onOk: () => {
                                                util.ajax.post('/busine/prizes/delete', util.toFormData({
                                                    id: params.row.id
                                                })).then(res => {
                                                    if (res.data.message === 'SUCCESS') {
                                                        this.$Message.success('删除成功！');
                                                        this.loadList();
                                                    }
                                                });
                                            },
                                            onCancel: () => {
                                            }
                                        });
                                    }
                                }
                            }, '删除'),
                            h('Button', {
                                props: {
                                    type: 'primary',
                                    size: 'small'
                                },
                                style: {
                                    marginRight: '5px'
                                },
                                on: {
                                    click: () => {
                                        util.toPDF(CONSTANTS.SERVER_HOST + params.row.imgpath, params.row.theme.name + '-' + params.row.diplomas.name);
                                    }
                                }
                            }, '下载DPF')
                        ]);
                    }
                }],
            baseuser: JSON.parse(Cookies.get('baseUser')),
            data: [],
            query: {
                realname: '',
                phone: ''
            },
            total: 0,
            pageIndex: 1,
            pageSize: 10,
            memberModal: false,
            newIds: [],
            showImg: false,
            imgUrl: '',
            prizeId: '',
            ids: []
        };
    },
    mounted () {
        this.loadList();
    },
    methods: {
        loadList () {
            util.ajax.post('/busine/prizes/list', util.toFormData({
                page: this.pageIndex,
                size: this.pageSize,
                name: this.query.realname,
                phone: this.query.phone,
                orgid: this.baseuser.orgId
            })).then(res => {
                this.data = res.data.data.list;
                this.total = res.data.data.total;
            });
        },
        refresh () {
            this.query = {
                realname: '',
                phone: ''
            };
            this.loadList();
        },
        onSelect (memberIds) {
            this.newIds = memberIds;
        },
        save () {
            util.ajax.post('/busine/prizes/prizeAllot', util.toFormData({
                prizeId: this.prizeId,
                memberids: this.newIds
            })).then(res => {
                if (res.data.message === 'SUCCESS') {
                    this.$Message.success('会员设置成功！');
                } else {
                    this.$Message.warning(res.data.message || '未知错误！');
                }
                this.memberModal = false;
            });
        },
        getSelect (ref) {
            this.ids = [];
            ref.forEach((item) => {
                this.ids.push(item.id);
            });
        },
        deleteByIds () {
            if (this.ids.length > 0) {
                this.$Modal.confirm({
                    title: '提示',
                    content: '<p>确定删除所选奖状？</p>',
                    onOk: () => {
                        util.ajax.post('/busine/prizes/deleteByIds', util.toFormData({
                            ids: this.ids
                        })).then(res => {
                            if (res.data.message === 'SUCCESS') {
                                this.$Message.success('删除成功！');
                                this.loadList();
                            }
                        });
                    }
                });
            } else {
                this.$Modal.error({
                    content: '请选择要删除奖状！'
                });
            }
        }
    }
}